package com.iptv.grape.domain;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.ui.control.ad.ListViewUI;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.ReadJsonFile;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
// 频道类别实体类
public class ChannelType implements Comparable<ChannelType>,IListViewShowItem{

    // 当前的模式 live = 直播  his = 回看
    private static String curType = "live";

    public static String getCurType() {
        return curType;
    }

    public static void setCurType(String type) {
        curType = type;
    }

    // 频道类别ID(唯一)
    private String keyID;
    // 频道类别编码(唯一)
    private String code;
    // 频道类别名称
    private String name;
    // 频道类别排序码(从小到大)
    private int sort;
    // 站点编码
    private String siteCode;




    public ChannelType(){

    }



    public String getKeyID() {
        return keyID;
    }

    public void setKeyID(String keyID) {
        this.keyID = keyID;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSort() {
        return sort;
    }

    @Override
    public String getText() {
        return this.getName();
    }

    @Override
    public void itemClick(List<IListViewShowItem> dataSouce, int pos, ListViewUI listViewUI) {

    }

    @Override
    public List<IListViewShowItem> getChList() {
        ChannelDLinkList channelDLinkList = GrapeContext.curGrapeContext().getChannelDLinkList();
        List<IListViewShowItem> list = new ArrayList<IListViewShowItem>();
        List<Channel> channels = null;
        if(curType.equals("live")) {
            channels = channelDLinkList.seachByCategoryCode(this.code);
        }else {
            channels = channelDLinkList.seachByCategoryCodeAndHis(this.code);
        }
        list.addAll(channels);
        return list;
    }
    @Override
    public ListViewStyle getListViewStyle(){

        if(AppConstant.isTvStb()) {
            return new ListViewStyle(100, "channelTypeListItem",
                    "ChannelType", 9, 4, 2, 55, null);
        }
        else {
            return new ListViewStyle(100, "channelTypeListItem",
                    "ChannelType", 11, 5, 2, 55, null);
        }
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    public String getSiteCode() {
        return siteCode;
    }

    public void setSiteCode(String siteCode) {
        this.siteCode = siteCode;
    }

    @Override
    public int compareTo(ChannelType channelType) {
        return this.sort - channelType.sort;
    }

    public static void serializeToFile(List<ChannelType> channelTypeList){
        Gson gson = new Gson();
        String json = gson.toJson(channelTypeList);
        ReadJsonFile.writeFile("channelType.json",json);
    }

    public static List<ChannelType> unSerializeToFile(){
        List<ChannelType> channelTypeList = null;
        try {
            if(ReadJsonFile.fileExists("channelType.json")) {
                String json = ReadJsonFile.readFile("channelType.json");
                Type type = new TypeToken<List<ChannelType>>() {
                }.getType();
                channelTypeList = new Gson().fromJson(json, type);
                Collections.sort(channelTypeList);
            }
            return channelTypeList;
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
        return channelTypeList;
    }
}
